/**
 * 
 */
CartPage = {
		'CalcTotalAmount': function(){

			var lst_sku = new Array();

			$('.cart_list li').each(function(){
				$qty = $(this).find('.qty').val();
				if($qty > 0){
					var sku_id = $(this).attr('sku_id');
					lst_sku.push(sku_id + "," + $qty);
				}
			});
			var lst_sku_str	=lst_sku.join('|');

			var result		= OrderService.CalcCartAmount(lst_sku_str);
			if(result.IsSuccess == 0){
				alert(result.ErrorMsg);
				return;
			}
			else{

				var goods_amount 		= parseFloat(result.Result.goods_amount);
				var promotion_amount 	= parseFloat(result.Result.promotion_amount);
				var no_freight_order_amount	= parseFloat(result.Result.no_freight_order_amount);
				
				$('.sku_amount').text(goods_amount.toFixed(2));
				$('.discount_amount').text(promotion_amount.toFixed(2));
				$('.total_amount').text(no_freight_order_amount.toFixed(2));
			}
		},
		'BindEvent': function(){
			//点击加号
			$('.add').bind('click', function(){
				var curr_qty = $(this).parents('li').find('.qty').val();
				if(!isNaN(curr_qty)){
					var qty = parseInt(curr_qty) + 1;
					$(this).parents('li').find('.qty').val(qty);
				}else{
					$(this).parents('li').find('.qty').val('0');
				}
				
				CartPage.CalcTotalAmount();
			});
			//点击减号
			$('.reduce').bind('click', function(){
				var curr_qty = $(this).parents('li').find('.qty').val();
				if(!isNaN(curr_qty)){
					if(curr_qty > 0){
						var qty = parseInt(curr_qty) - 1;
						$(this).parents('li').find('.qty').val(qty);
					}
					
					
				}else{
					$(this).parents('li').find('.qty').val('0');
				}
				CartPage.CalcTotalAmount();
			});
			//点击删除
			$('.del_btn').bind('click', function(){
				$(this).parents('li').find('.qty').val('0');
				CartPage.CalcTotalAmount();
			});
			//qty值改变事件
			$('.qty').bind('change', function(){
				CartPage.CalcTotalAmount();
			});
			//结算
			$('.check_total').bind('click', function(){
				var share_code = $('.cart_list').attr('share_code');
				
				var lst_sku = new Array();
				
				$('.cart_list li').each(function(){
					$qty = $(this).find('.qty').val();
					if($qty > 0){
						var sku_id = $(this).attr('sku_id');
						lst_sku.push(sku_id + "," + $qty);
					}
				});
				if(lst_sku.length == 0){
					alert('请选择一件商品购买');
					return;
				}
				
				
				var url = ServerConfig.UrlConfig.checkout_url + "?lst_sku=" + lst_sku.join('|');
				window.location.href = url;
			});
			
		},
};

$(document).ready(function(){
	CartPage.BindEvent();
	CartPage.CalcTotalAmount();
});


